package com.woniu113.junk.site.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu113.junk.commons.model.Result;
import com.woniu113.junk.commons.util.RedisUtil;
import com.woniu113.junk.commons.util.TokenUtil;
import com.woniu113.junk.site.dto.AllNoticeDTO;
import com.woniu113.junk.site.form.notice.AddNoticeForm;
import com.woniu113.junk.site.form.notice.AllNoticeForm;
import com.woniu113.junk.site.form.notice.UpdateNoticeForm;
import com.woniu113.junk.site.model.Notice;
import com.woniu113.junk.site.model.WasteType;
import com.woniu113.junk.site.param.notice.AddNoticeParam;
import com.woniu113.junk.site.param.notice.AllNoticeParam;
import com.woniu113.junk.site.param.notice.UpdateNoticeParam;
import com.woniu113.junk.site.param.wastetype.AllWasteTypeParam;
import com.woniu113.junk.site.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 蜡笔没了小新
 * @since 2024年10月16日
 */
@RestController
@RequestMapping("/notice")
@Api(value = "NoticeController", tags = "公告信息接口")
public class NoticeController {
    @Resource
    private NoticeService noticeService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 新增公告信息
     * @param form 新增公告信息的内容
     * @param token 当前登录管理员的token,用于获取当前登录的adminId
     * @return 返回新增的公告数据和成功提示与code
     * @throws Exception
     */
    @PostMapping("/addNotice")
    @ApiOperation(value = "新增公告信息")
    public Result<Notice> addNotice(@RequestBody AddNoticeForm form,@RequestHeader String token) throws Exception {
        System.out.println(form.getNoticeTitle());
        System.out.println(form.getNoticeContent());
        //获取当前登录管理员的id
        int adminId = Integer.parseInt(TokenUtil.parseToken(redisUtil.get(token)).get("userId").toString());
        System.out.println(adminId);
        Notice newNotice = noticeService.addNotice(BeanUtil.toBean(form, AddNoticeParam.class),adminId);
        return Result.getResult(Notice.class).setData(newNotice).setMsg("新增公告成功").setCode(200);
    }

    /**
     * 修改公告
     * @param form 要修改的公告信息
     * @param token 获取当前登录用户的id
     * @return 返回成功提示与code
     * @throws Exception
     */
    @PostMapping("/updateNotice")
    @ApiOperation(value = "修改公告信息")
    public Result updateNotice(@RequestBody UpdateNoticeForm form,@RequestHeader String token) throws Exception {
        //获取当前登录管理员的id
        int adminId = Integer.parseInt(TokenUtil.parseToken(redisUtil.get(token)).get("userId").toString());
        System.out.println(adminId);
        noticeService.updateNotice(BeanUtil.toBean(form, UpdateNoticeParam.class),adminId);
        return Result.getResult().setMsg("修改公告成功").setCode(200);
    }

    /**
     * 发布公告信息
     * @param noticeId 要发布的公告id
     * @return 返回成功提示与code
     */
    @GetMapping("/upNotice")
    @ApiOperation(value = "发布公告信息")
    public Result upNotice(@RequestParam("noticeId") Integer noticeId){
        System.out.println("公告id"+noticeId);
        noticeService.upNotice(noticeId);
        return Result.getResult().setMsg("发布公告成功").setCode(200);
    }

    /**
     * 删除公告信息
     * @param noticeId 要删除的公告id
     * @return 返回成功提示与code
     */
    @GetMapping("/deleteNotice")
    @ApiOperation(value = "删除公告信息")
    public Result deleteNotice(@RequestParam("noticeId") Integer noticeId){
        System.out.println("公告id"+noticeId);
        noticeService.deleteNotice(noticeId);
        return Result.getResult().setMsg("删除公告成功").setCode(200);
    }

    /**
     * 按公告id查询公告信息
     * @param noticeId 公告id
     * @return 返回成功提示与code
     */
    @GetMapping("/findNoticeById")
    @ApiOperation(value = "按公告id查询公告信息")
    public Result<Notice> findNoticeById(@RequestParam("noticeId") Integer noticeId){
        Notice notice = noticeService.getById(noticeId);
        return Result.getResult(Notice.class).setData(notice).setMsg("按id查询公告信息成功").setCode(200);
    }

    /**
     * 查询全部公告信息
     * @param form 要查询的公告信息条件
     * @return 返回成功提示与code
     */
    @PostMapping("/findAll")
    @ApiOperation(value = "查询全部公告信息")
    public Result<List<Notice>> findAll(@RequestBody AllNoticeForm form){
        System.out.println(form.getPageNum());
        System.out.println(form.getPageNum());
        System.out.println(form.getNoticeState());
        System.out.println(form.getNoticeTitle());
        int offset = (form.getPageNum()-1) *form.getPageSize();
        AllNoticeDTO dto = noticeService.findAll(BeanUtil.toBean(form, AllNoticeParam.class),offset);
        return Result.getListResult(Notice.class).setData(dto.getList()).setMsg("查询所有公告信息成功").setCode(200).setTotal(dto.getTotal());
    }
}

